package com.zzzm.hospital.customerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zzzm.hospital.R;

public class AppointmentView extends FrameLayout {

    private int image;
    private String text;

    private View view;
    private ImageView imageView;
    private TextView textView;

    public AppointmentView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public AppointmentView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AppointmentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public AppointmentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        //获取自定义属性集合
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.appointmentview);
        image = typedArray.getResourceId(R.styleable.appointmentview_image_appointment, 0);
        text = typedArray.getString(R.styleable.appointmentview_text_appointment);
        //释放掉typedArray
        typedArray.recycle();

        //绑定layout布局
        view = LayoutInflater.from(context).inflate(R.layout.appointment_view, this, false);
        imageView = view.findViewById(R.id.view_image);
        textView = view.findViewById(R.id.view_text);

        //布局关联属性
        imageView.setImageResource(image);
        textView.setText(text);

        //将这个view给ModelView
        addView(view);
    }
}
